package cn.tannn.tcache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 优雅启停
 *
 * @author <a href="https://t.tannn.cn/">tnnn</a>
 * @version V1.0
 * @date 2024/6/12 下午9:24
 */
@Component
public class TanApplicationListener implements ApplicationListener<ApplicationEvent> {

    @Autowired
    List<TanPlugin> plugins;

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if(event instanceof ApplicationReadyEvent are){
            // 项目启动处理
            plugins.forEach(plugin -> {
                plugin.init();
                plugin.startup();
            });
        }else if (event instanceof ContextClosedEvent cce){
            // 项目停止处理
            plugins.forEach(TanPlugin::shutdown);
        }
    }
}
